package com.xckj.teacher.presenter

import com.blankj.utilcode.util.ToastUtils
import com.xckj.teacher.bean.BaseBean
import com.xckj.teacher.bean.ClassBean
import com.xckj.teacher.bean.ClassDetailBean
import com.xckj.teacher.bean.GradeResetPassBean
import com.xckj.teacher.bean.HomeWorkBean
import com.xckj.teacher.bean.RoomReportBean
import com.xckj.teacher.framework.IBaseView
import com.xckj.teacher.framework.net.RetrofitManager
import com.xckj.teacher.global.Constant
import com.xckj.teacher.iview.IClassView
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.Objects

/**
 * @Author: ChenYao
 * @Describe: 首页
 * @Date: 2024/4/11 15:14
 */
class ClassPresenter(private val classView: IBaseView) {

    fun classList(success: (List<ClassBean>) -> Unit, fail: () -> Unit) {
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.getClassList()
        call.enqueue(object : Callback<BaseBean<List<ClassBean>>> {
            override fun onResponse(
                call: Call<BaseBean<List<ClassBean>>>,
                response: Response<BaseBean<List<ClassBean>>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success(baseBean.data)
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                        fail()
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                    fail()
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<List<ClassBean>>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                fail()
                classView.hideLoading()
            }
        })
    }

    fun editGrade(id: String, name: String, success: () -> Unit) {
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.editGrade(id, name)
        call.enqueue(object : Callback<BaseBean<Any>> {
            override fun onResponse(
                call: Call<BaseBean<Any>>,
                response: Response<BaseBean<Any>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success()
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<Any>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                classView.hideLoading()
            }
        })
    }

    fun addGroup(ids: String, name: String, classId: String, success: () -> Unit) {
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.addGroup(ids, name, classId)
        call.enqueue(object : Callback<BaseBean<Any>> {
            override fun onResponse(
                call: Call<BaseBean<Any>>,
                response: Response<BaseBean<Any>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success()
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<Any>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                classView.hideLoading()
            }
        })
    }

    fun homeWork(
        start: String,
        end: String,
        class_id: String,
        type: String,
        success: (HomeWorkBean) -> Unit
    ) {
        //1=听说模考,2=单词跟读,3=课文跟读,4=线上做答
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.homework(start, end, class_id, type)
        call.enqueue(object : Callback<BaseBean<HomeWorkBean>> {
            override fun onResponse(
                call: Call<BaseBean<HomeWorkBean>>,
                response: Response<BaseBean<HomeWorkBean>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success(baseBean.data)
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<HomeWorkBean>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                classView.hideLoading()
            }
        })
    }
    fun machine(
        start: String,
        end: String,
        class_id: String,
        success: (RoomReportBean) -> Unit
    ) {
        //1=听说模考,2=单词跟读,3=课文跟读,4=线上做答
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.machine(start, end, class_id)
        call.enqueue(object : Callback<BaseBean<RoomReportBean>> {
            override fun onResponse(
                call: Call<BaseBean<RoomReportBean>>,
                response: Response<BaseBean<RoomReportBean>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success(baseBean.data)
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<RoomReportBean>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                classView.hideLoading()
            }
        })
    }

    fun deleteGroup(ids: String, success: () -> Unit) {
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.deleteGroup(ids)
        call.enqueue(object : Callback<BaseBean<Any>> {
            override fun onResponse(
                call: Call<BaseBean<Any>>,
                response: Response<BaseBean<Any>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success()
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<Any>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                classView.hideLoading()
            }
        })
    }

    fun gradeResetPass(id: String, success: (String) -> Unit) {
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.gradeResetPass("5")
        call.enqueue(object : Callback<BaseBean<GradeResetPassBean>> {
            override fun onResponse(
                call: Call<BaseBean<GradeResetPassBean>>,
                response: Response<BaseBean<GradeResetPassBean>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success(baseBean.data.password)
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<GradeResetPassBean>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                classView.hideLoading()
            }
        })
    }

    fun classDetail(id: String, success: (ClassDetailBean) -> Unit) {
        classView.showLoading()
        val call = RetrofitManager.getInstance().service.getClassDetail(id)
        call.enqueue(object : Callback<BaseBean<ClassDetailBean>> {
            override fun onResponse(
                call: Call<BaseBean<ClassDetailBean>>,
                response: Response<BaseBean<ClassDetailBean>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success(baseBean.data)
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
                classView.hideLoading()
            }

            override fun onFailure(call: Call<BaseBean<ClassDetailBean>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
                classView.hideLoading()
            }
        })
    }

}
